package net.monare.mifss.admin.ui.valid;

import org.apache.commons.lang.StringUtils;

import com.vaadin.data.validator.AbstractValidator;

public class IntIdOrEmptyValidator extends AbstractValidator<String> {

	private static final long serialVersionUID = -8431069234415926095L;

	public IntIdOrEmptyValidator(String errorMessage) {
		super(errorMessage);
	}

	@Override
	protected boolean isValidValue(String value) {

		int converted = 0;

		if (StringUtils.isNotEmpty(value)) {

			try {
				converted = Integer.parseInt(value);

				if (converted > 0) {
					return true;
				} else {
					return false;
				}

			} catch (Exception e) {
				return false;
			}

		} else {

			// non mandatory
			return true;
		}

	}

	@Override
	public Class<String> getType() {
		return String.class;
	}

}
